<template>
	<div >
		<el-card shadow="hover">
			<div class=" mb15">
				<el-form :model="tableData.param" ref="queryRef" :inline="true" label-width="100px">
					<el-row>
						<el-col :span="6" class="colBlock">
							<el-form-item label="api名称" prop="apiName">
								<el-input v-model="tableData.param.apiName" placeholder="请输入api名称" clearable
									@keyup.enter.native="getList" />
							</el-form-item>
						</el-col>
						<el-col :span="6" class="colBlock">
							<el-form-item label="分组" prop="groupCode">
								<el-select v-model="tableData.param.groupCode" placeholder="请选择设备分组" clearable>
									<el-option v-for="item in configGroupData" :key="item.code" :label="item.name"
										:value="item.code" />
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :span="6" class="colBlock">
							<el-form-item label="分组状态" prop="status">
								<el-select v-model="tableData.param.status" placeholder="请选择设备分组" clearable>
									<el-option label="开放" :value="true" />
									<el-option label="不开放" :value="false" />
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :span="6" :class="!showAll ? 'colBlock' : 'colNone'">
							<el-form-item>
								<el-button type="primary" @click="getList"><el-icon>
										<Search />
									</el-icon>搜索</el-button>
								<el-button @click="resetQuery(queryRef)"><el-icon>
										<Refresh />
									</el-icon>重置</el-button>
							</el-form-item>
						</el-col>
					</el-row>
				</el-form>
			</div>
			<el-table v-loading="loading" :data="tableData.data">
				<el-table-column label="配置名称" align="center" prop="name" min-width="100px">
					<template #default="scope">
						{{ scope.row.api.apiName }}
					</template>
				</el-table-column>
				<el-table-column label="配置组分组" align="center" prop="name" min-width="100px">
					<template #default="{ row }">
						<el-tag v-for="tag in row.group" :key="tag" class="tag-item ml5" type="primary">
							{{ tag.groupName }}
						</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="是否开放" align="center" prop="name" min-width="100px">
					<template #default="scope">
						<el-switch v-model="scope.row.openStatus" class="mb-2"
							@change="handleStatusChange(scope.row)" />
					</template>
				</el-table-column>
				<el-table-column v-if="permissionStore.isPermissionsBtn('003001001')" label="操作" align="center" class-name="small-padding" min-width="200px" fixed="right">
					<template #default="scope">
						<el-button type="primary" link @click="handleUpdate(scope.row)"><el-icon>
								<EditPen />
							</el-icon>修改</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>
		<EditView ref="editRef"   @getList="getList"></EditView>
	</div>
</template>
<script lang="ts">
import { configListApi, configGroupApi,editConfigStatusApi } from '@/api/thirdManage/apiConfiguration/index';
import { toRefs, reactive, onMounted, ref, defineComponent, computed, getCurrentInstance, toRaw } from 'vue';
import { ElMessageBox, ElMessage, FormInstance } from 'element-plus';
import { usePermissionStore } from '@/store/modules/permission'
import EditView from './component/edit.vue';
export default defineComponent({
	name: 'apiConfigurationList',
	components: {
		EditView,
	},
	setup() {
		const { proxy } = <any>getCurrentInstance();
		const loading = ref(false);
		const queryRef = ref();
		const addRef = ref();
		const editRef = ref();
		const detailRef = ref();
		// 是否显示所有搜索选项
		const showAll = ref(false);

		const word = computed(() => {
			if (showAll.value === false) {
				//对文字进行处理
				return '展开搜索';
			} else {
				return '收起搜索';
			}
		});
		const permissionStore = usePermissionStore()
		const state = reactive({
			ids: [],
			tableData: {
				data: [],
				loading: false,
				param: {
					apiName: '',
					groupCode: '',
					status: undefined
				},
			},
		});
		// 页面加载时
		onMounted(() => {
			initTableData();
			configGroup()
		});
		// 初始化表格数据
		const initTableData = () => {
			getList();
		};
		/** 重置按钮操作 */
		const resetQuery = (formEl: FormInstance | undefined) => {
			if (!formEl) return;
			formEl.resetFields();
			getList();
		};
		// 获取列表数据
		const getList = () => {
			loading.value = true;

			configListApi(state.tableData.param).then((res: any) => {
				let list = res.data ?? [];
				state.tableData.data = list;
				console.log(state.tableData.data);
				loading.value = false;
			});
		};
		// 修改
		const handleUpdate = (row) => {
			editRef.value.openDialog(toRaw(row));
		};
		// 获取配置组数据
		const configGroupData = ref([])
		const configGroup = () => {
			configGroupApi().then((res: any) => {
				if (res.data) configGroupData.value = res.data
			})
		}

		// 用户状态修改
		const handleStatusChange = (row: any) => {
			let text = row.openStatus? "开放" : "关闭";
			ElMessageBox.confirm('确认要"' + text + '"："' + row.api.apiName + '"配置组吗?', "警告", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				type: "warning"
			}).then(function () {
				return editConfigStatusApi({code:row.api.apiCode,status:row.openStatus});
			}).then(() => {
				ElMessage.success(text + "成功");
			}).catch(function () {
				row.openStatus = row.openStatus?false : true;
			});
		};
		return {
			proxy,
			addRef,
			editRef,
			detailRef,
			showAll,
			loading,
			word,
			queryRef,
			configGroupData,
			permissionStore,
			resetQuery,
			getList,
			handleUpdate,
			handleStatusChange,
			...toRefs(state),
		};
	},
});
</script>
<style lang="scss" scoped>
.colBlock {
	display: block;
}

.colNone {
	display: none;
}
</style>
